package demo.module_main.v.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.widget.Toast;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.roughike.bottombar.BottomBar;
import com.roughike.bottombar.OnTabReselectListener;
import com.roughike.bottombar.OnTabSelectListener;
import com.roughike.bottombar.TabSelectionInterceptor;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import demo.module_main.R;
import demo.module_main.R2;
import demo.module_main.p.MainPresenter;
import demo.module_main.v.view.CustomViewPager;
import library.base.mvp.presenter.event.BaseEvent;
import library.base.mvp.v.activity.BaseAppCompatActivity;
import library.base.mvp.v.fragment.BaseFragment;
import library.common.util.LogUtils;
import library.common.util.ToastUtils;
import library.router.Router;
import module.common.RouterConfig;
import module.common.provider.IModuleOneProvider;

/**
 * BottomBar + ViewPager + Fragments can scroll or not
 *
 * @author Created by qiang.hou on 2018/3/3.
 * @version 1.0
 */
@Route(path = "/module_main/MainActivity")
public class AppBottomBarViewPagerActivity extends BaseAppCompatActivity<MainPresenter> {
    private static final String STATE_BOTTOM_BAR = "STATE_BOTTOM_BAR";
    private static final int INDEX_TAB_ONE = 0;
    private static final int INDEX_TAB_TWO = 1;

    @BindView(R2.id.contentContainer)
    CustomViewPager container;
    @BindView(R2.id.bottomBar)
    BottomBar bottomBar;

    /**
     * for fragments list
     */
    private List<BaseFragment> fragmentList = new ArrayList<>();

    /**
     * the current tab position of bottom bar
     */
    private int currentTabPosition = INDEX_TAB_ONE;

    @OnClick(R2.id.button)
    public void onButtonClick() {
        IModuleOneProvider provider = Router.getInstance().getService("module_one.DemoService");
        if (null != provider) {
            ToastUtils.showToast(this, provider.getModuleName(), Toast.LENGTH_SHORT);
        }
    }

    @Override
    protected void initView() {
        fragmentList.clear();

        BaseFragment moduleOneFragment = Router.getInstance().getFragment("module_one.ModuleOneFragment");
        if (null != moduleOneFragment) {
            fragmentList.add(moduleOneFragment);
        }

        BaseFragment moduleSettingFragment = Router.getInstance().getFragment(RouterConfig.setting_ModuleSettingFragment);
        if (null != moduleSettingFragment) {
            fragmentList.add(moduleSettingFragment);
        }

        // true:viewpager can scroll; false:viewpager no scroll
        container.setScanScroll(true);
        container.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return fragmentList.get(position);
            }

            @Override
            public int getCount() {
                return fragmentList.size();
            }
        });
        // 设置控制fragment不重新走生命周期的个数的，打个比方一共4个fragment页面，如果mViewPager.setOffscreenPageLimit(3)，那么所有的fragment都只走一次生命周期
        container.setOffscreenPageLimit(1);
        bottomBar.selectTabAtPosition(currentTabPosition);
    }

    @Override
    protected <E extends BaseEvent> void initDataView(E event) {

    }

    @Override
    protected <E extends BaseEvent> void onHandleMessage(E event) {

    }

    @Override
    protected void setListener() {
        if (null != container) {
            container.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                @Override
                public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                }

                @Override
                public void onPageSelected(int position) {
                    if (currentTabPosition == position) {
                        return;
                    }
                    bottomBar.selectTabAtPosition(position, true);
                }

                @Override
                public void onPageScrollStateChanged(int state) {

                }
            });

        }
        if (null != bottomBar) {
            bottomBar.setOnTabSelectListener(new OnTabSelectListener() {
                @Override
                public void onTabSelected(int tabId) {
                    if (tabId == R.id.tab_1) {
                        currentTabPosition = INDEX_TAB_ONE;

                    } else if (tabId == R.id.tab_2) {
                        currentTabPosition = INDEX_TAB_TWO;

                    }
                    // true: 切换有滑动效果; false：切换无滑动效果
                    container.setCurrentItem(currentTabPosition, true);
                }
            });
            bottomBar.setOnTabReselectListener(new OnTabReselectListener() {
                @Override
                public void onTabReSelected(int tabId) {
                    if (tabId == R.id.tab_1) {
                    } else if (tabId == R.id.tab_2) {
                    }
                }
            });

            bottomBar.setTabSelectionInterceptor(new TabSelectionInterceptor() {
                @Override
                public boolean shouldInterceptTabSelection(int oldTabId, int newTabId) {
                    if (newTabId == R.id.tab_1) {
                    } else if (newTabId == R.id.tab_2) {
                    }
                    return false;
                }
            });
        }
    }

    @Override
    protected int returnLayoutId() {
        return R.layout.layout_for_viewpager_main_activity;
    }

    @Override
    protected MainPresenter createPresenter() {
        return new MainPresenter();
    }

    @Override
    protected boolean needEventBus() {
        return false;
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        LogUtils.i("sky_dreaming on onSaveInstanceState");
        outState.putBundle(STATE_BOTTOM_BAR, (Bundle) bottomBar.onSaveInstanceState());
        super.onSaveInstanceState(outState);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        LogUtils.i("sky_dreaming on onRestoreInstanceState");
        Bundle bottomBundle = savedInstanceState.getBundle(STATE_BOTTOM_BAR);
        bottomBar.onRestoreInstanceState(bottomBundle);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
    }
}
